﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BusinessEntities;
using BusinessServices;
using WebAPI.Filters;
using WebAPI.ActionFilters; 

namespace WebAPI.Controllers
{

    [AuthorizationRequired]
    public class RegionController : ApiController
    {
        private readonly IRegionServices _regionServices;
         #region Public Constructor

        /// <summary>
        /// Public constructor to initialize product service instance
        /// </summary>
        public RegionController(IRegionServices service)
        {
            _regionServices = service;
        }

        #endregion

        // GET api/product
        public HttpResponseMessage Get()
        {
            var obj = _regionServices.GetAllRegions();
            if (obj != null)
            {
                var entities = obj as List<RegionEntity> ?? obj.ToList();
                if (entities.Any())
                    return Request.CreateResponse(HttpStatusCode.OK, entities);
            }
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Object not found");
        }

        // GET api/product/5
        public HttpResponseMessage Get(int id)
        {
            var obj = _regionServices.GetRegionById(id);
            if (obj != null)
                return Request.CreateResponse(HttpStatusCode.OK, obj);
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No object found for this id");
        }

        // POST api/product
        public int Post([FromBody] RegionEntity entity)
        {
            return _regionServices.CreateRegion(entity);
        }

        // PUT api/product/5
        public bool Put(int id, [FromBody]RegionEntity entity)
        {
            if (id > 0)
            {
                return _regionServices.UpdateRegion(id, entity);
            }
            return false;
        }

        // DELETE api/product/5
        public bool Delete(int id)
        {
            if (id > 0)
                return _regionServices.DeleteRegion(id);
            return false;
        }
    }
}
